var util=require("../../utils/util.js");
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      inTheaters:{},
      comingSoon:{},
      top250:{},
      containerShow:true,
      searchShow:false,
      searchResult:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取热映电影
    var inTheatersUrl="/v2/movie/in_theaters?start=0&count=3";
    //获取最新电影
    var comingSoonUrl ="/v2/movie/coming_soon?start=0&count=3";
    //获取TOP250电影
    var top250Url ="/v2/movie/top250?start=0&count=3";
    this.getMovieListData(inTheatersUrl,"inTheaters");
    this.getMovieListData(comingSoonUrl,"comingSoon");
    this.getMovieListData(top250Url,"top250");
  },
  onMoreMovie:function(event){
    var category = event.currentTarget.dataset.category;
    wx.navigateTo({
        url: 'movie-more/movie-more?category=' + category,
    })
  },
  ontapDetail:function(event){
    var movieId = event.currentTarget.dataset.movieid;
    wx.navigateTo({
        url: 'movie-detail/movie-detail?movieId=' + movieId,
    })
  },
  getMovieListData:function(url,types){
    var that=this;
    //获取电影数据
    wx.request({
        url: app.globalData.doubanBase + url,
        method: 'GET',
        header: { 'Content-type': 'json' },
        success: function(res){
            that.processDoubanData(res.data,types);
        },
        fail: function () {
            console.log("获取电影数据失败");
        },
        complete: function () { }
    })
  },
  onBindFocus:function(event){
    console.log("show search");
    this.setData({
        containerShow:false,
        searchShow:true
    })
  },
  onBindChange:function(event){
    var text=event.detail.value;
    console.info(text);//获取文本框值
    var searchUrl=app.globalData.doubanBase+"/v2/movie/search?q="+text;
    //获取电影数据
    wx.request({
        url: searchUrl,
        method: 'GET',
        header: { 'Content-type': 'json' },
        success:(res)=>{this.processDoubanData(res.data,"searchResult");},
        fail:()=>{console.log("获取电影数据失败");},
        complete:()=>{}
    })
  },
  onCancelImage:function(event){
    this.setData({
        containerShow:true,
        searchShow:false,
        searchResult:{}
    })
  },
  processDoubanData:function(moviesDouban,types){
    var movies=[];
    for(var subject of moviesDouban.subjects){
            var title=subject.title;
            if(title.length>=6){
                title=title.substring(0,6)+"...";
            }
            var temp={
                stars:util.convertToStarsArray(subject.rating.stars),
                title:title,
                average:subject.rating.average,
                coverageUrl:subject.images.large,
                movieId:subject.id
            };
        movies.push(temp);
    }
    var result={};
    result[types] = { movies: movies, title: moviesDouban.title};
    this.setData(result);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})